# +----------------------------------------------------------------------
# | Pink [ A modern python web framework ]
# +----------------------------------------------------------------------
# | Copyright (c) 2023 http:#unnnnn.com All rights reserved.
# +----------------------------------------------------------------------
# | Author: chenjianhua <unnnnn@foxmail.com>
# +----------------------------------------------------------------------

import builtins

from src.vendor.exceptiondump import Handler
from src.vendor.exceptiondump.options import DEFAULT_OPTIONS
from src.vendor.exceptiondump.renderers import JSONRenderer
from src.vendor.exceptiondump.solutions import PythonSolutions

from .Provider import Provider
from ..configuration import config
from ..dumps import Dumper
from ..exceptions import (
    DumpExceptionHandler,
    HttpExceptionHandler,
    ModelNotFoundHandler,
)
from ..exceptions.ExceptionHandler import ExceptionHandler
from ..exceptions.exceptionite import solutions
from ..exceptions.exceptionite.blocks import RequestBlock, AppBlock, ConfigBlock
from ..exceptions.exceptionite.controllers import ExceptioniteController
from ..exceptions.exceptionite.tabs import DumpsTab
from ..routes import Route


class ExceptionProvider(Provider):
    def __init__(self, application):
        self.application = application

    def register(self):
        exceptionite = Handler()
        options = config("exceptions")
        # @removed:5.0.0
        # old project won't have new options for exceptions
        if not options.get("options.editor"):
            options = DEFAULT_OPTIONS
        exceptionite.set_options(options)

        # configure exceptiondump for PinkPy specifically
        exceptionite.app = self.application
        self.application.make("router").add(
            Route.group(
                [
                    Route.post(
                        "/_exceptiondump/actions/", ExceptioniteController.run_action
                    )
                ]
            )
        )
        exceptionite.add_renderer("json", JSONRenderer)
        exceptionite.renderer("web").add_tabs(DumpsTab)
        exceptionite.renderer("web").tab("context").add_blocks(
            RequestBlock, AppBlock, ConfigBlock
        )
        exceptionite.renderer("web").tab("solutions").block(
            "possible_solutions"
        ).register(
            solutions.TableNotFound(),
            solutions.MissingCSRFToken(),
            solutions.InvalidCSRFToken(),
            solutions.TemplateNotFound(),
            solutions.NoneResponse(),
            solutions.RouteMiddlewareNotFound(),
            *PythonSolutions.get()
        )

        exception_handler = ExceptionHandler(self.application)
        exception_handler.add_driver("exceptiondump", exceptionite)
        self.application.bind("exception_handler", exception_handler)

        # dumper
        dumper = Dumper(self.application)
        self.application.bind("dumper", dumper)
        builtins.dd = dumper.dd
        builtins.dump = dumper.dump
        builtins.clear_dumps = dumper.clear
        self.application.bind(
            "DumpExceptionHandler", DumpExceptionHandler(self.application)
        )
        self.application.bind(
            "HttpExceptionHandler", HttpExceptionHandler(self.application)
        )
        self.application.bind(
            "ModelNotFoundHandler", ModelNotFoundHandler(self.application)
        )

    def boot(self):
        pass
